package com.itheima.reggie.srevice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.srevice.DishFlavorService;
import com.itheima.reggie.srevice.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 新增菜品，同时保留菜品口味
     * @param dishDto
     */
    @Override

    public void saveWithFlavor(DishDto dishDto) {

        //将菜品信息保存到dish表
        this.save(dishDto);

        Long dishId = dishDto.getId();

        //菜品分类
        List<DishFlavor> flavors = dishDto.getFlavors();

        //
        flavors.stream().map(item->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //将菜品口味保存到dishflavor表
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据Id查询菜品信息和菜品口味
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }


    /**
     * 更新菜品，同时更新菜品口味
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表的信息
        this.updateById(dishDto);

        //更新菜品口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //把原来的删除，在把现在的添加
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        List<DishFlavor> flavors = dishDto.getFlavors();
        //给菜品口味添加菜品ID
        flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 逻辑删除菜品信息和菜品口味
     * @param ids
     */
    @Override
    public void deleteWithFlavor(List<Long> ids) {

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);
        if (count>0){
            throw new CustomException("菜品正在售卖，不能删除！");
        }

        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(qw);

        dishService.removeByIds(ids);

    }

    /**
     * 改变菜品的状态
     * @param ids
     */
    @Override
    public void updateWithStatus(Integer status, List<Long> ids) {
        if (ids != null && ids.size() >0){
            for (long id : ids) {
                Dish one = dishService.getById(id);
                one.setStatus(status);
                dishService.updateById(one);
            }
        }
    }
}
